﻿using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class StudentBLL: IStudentBLL
    {
        public StudentBLL(IBaseDAL<Student> studentDAL)
        {
            StudentDAL = studentDAL;
        }

        public IBaseDAL<Student> StudentDAL { get; }

        public async Task<int> AddStudent(Student student)
        {
            var m= StudentDAL.GetAllAsync().Any(x=>x.StudentName==student.StudentName);
            if(!m)
            {
                return await StudentDAL.AddAsync(student);
            }
            else
            {
                return -1;
            }
                
        }

        public IQueryable<Student> GetStudents()
        {
            return StudentDAL.GetAllAsync();
        }

        public Student Login(string name, string pwd)
        {
            Student stu=StudentDAL.GetAllAsync().Where(x=>x.StudentName==name).FirstOrDefault();
            if(stu==null)
            {
                //账号不存在
                return new Student { StudentId = -1 };
            }
            else
            {
                if(stu.StudentPwd!=pwd)
                {
                    //密码错误
                    return new Student { StudentId = -2 };
                }
            }
            return stu;
        }

        public dynamic ShowStudent(int PageIndex, int PageSize)
        {
            var Stu = StudentDAL.GetAllAsync();
            var count=Stu.Count();
            var data = Stu.OrderBy(x => x.StudentId).Skip((PageIndex - 1) * PageSize).Take(PageSize);
            return new
            {
                count,
                data
            };
        }
    }
}
